﻿Imports System.Data.OleDb

Public Class Recipesubmit
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub BTN_recipeUpload_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BTN_recipeUpload.Click

        Dim myGUID As New System.Guid
        myGUID = Guid.NewGuid()
        Dim newFileName = myGUID.ToString() + ".jpg"

        Dim fileOK As Boolean = False
        If BTN_fileUpload.HasFile Then
            Dim fileExtension As String
            fileExtension = System.IO.Path.GetExtension(BTN_fileUpload.FileName).ToLower()
            Dim allowedExtensions As String() = {".jpg", ".jpeg"}
            For i As Integer = 0 To allowedExtensions.Length - 1
                If fileExtension = allowedExtensions(i) Then
                    fileOK = True
                End If

            Next
            If fileOK Then
                BTN_fileUpload.SaveAs(Request.MapPath("pictures") & "/" & newFileName)

            End If

        End If




        If fileOK Then
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
            Dim SqlString As String = "Insert into recipes(RecipeName, Description, MealType, Picture, Preparation_Time, Instructions, Submitted_by) Values (@f1,@f2,@f3,@f4,@f5,@f6,@f7)"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            Dim ratings As Integer
            Dim raters As Integer
            ratings = 0
            raters = 0
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", TB_recipeName.Text)
            cmd.Parameters.AddWithValue("@f2", TB_Desc.Text)
            cmd.Parameters.AddWithValue("@f3", DropDownList1.SelectedValue)
            cmd.Parameters.AddWithValue("@f4", "pictures/" & newFileName)
            cmd.Parameters.AddWithValue("@f5", DropDownList2.SelectedValue)
            cmd.Parameters.AddWithValue("@f6", TB_Inst.Text)
            cmd.Parameters.AddWithValue("@f7", User.Identity.Name)
            oleDbConn.Open()
            cmd.ExecuteNonQuery()
            Response.Redirect("Confirmation.aspx")
        Else
            Response.Redirect("wrongfile.aspx")
        End If

    End Sub
End Class